#include "atomic_0813_reminder_rules_service.h"


// 模拟存储规则的全局容器（实际项目中可能用数据库或更复杂的存储）
static std::vector<AlertRuleInput> g_alertRules;
static std::mutex g_rulesMutex;

int addAlertRule(AlertRuleInput *input, AlertRuleOutput *output) {
    // 检查输入输出指针有效性
    if (input == nullptr || output == nullptr) {
        if (output != nullptr) {
            output->return_type = ATOMIC_SERVICE_ERROR;
            output->message = "Invalid input/output pointer";
            output->log_level = 3; // ERROR级别
            output->content = "Input or output pointer is null";
            output->module_name = "atomic_0813_reminder_rules_service";
        }
        return ATOMIC_SERVICE_ERROR;
    }

    // 简单验证规则合法性
    if (input->rule_id <= 0) {
        output->return_type = ATOMIC_SERVICE_FAIL;
        output->message = "Invalid rule ID";
        output->log_level = 3; // ERROR级别
        output->content = "Rule ID must be positive integer";
        output->module_name = "atomic_0813_reminder_rules_service";
        return ATOMIC_SERVICE_FAIL;
    }

    if (input->rule_name.empty() || input->condition.empty() || input->action.empty()) {
        output->return_type = ATOMIC_SERVICE_FAIL;
        output->message = "Incomplete rule information";
        output->log_level = 3; // ERROR级别
        output->content = "Rule name, condition and action cannot be empty";
        output->module_name = "atomic_0813_reminder_rules_service";
        return ATOMIC_SERVICE_FAIL;
    }

    // 模拟添加规则（加锁防止并发问题）
    std::lock_guard<std::mutex> lock(g_rulesMutex);
    g_alertRules.push_back(*input);

    // 设置成功返回信息
    output->return_type = ATOMIC_SERVICE_SUCCESS;
    output->message = "Add rule success";
    output->log_level = 1; // INFO级别
    output->content = "Rule " + std::to_string(input->rule_id) + " (" + input->rule_name + ") has been added";
    output->module_name = "atomic_0813_reminder_rules_service";

    return ATOMIC_SERVICE_SUCCESS;
}